package org.zlpt.contract.dao;

import com.owlike.genson.Genson;
import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.zlpt.entity.co.EquipmentCO;


@Contract(
        name = "equipment"
)
public final class EquipmentContract implements ContractInterface {
    private final Genson genson = new Genson();

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public boolean EquipmentExists(final Context ctx, String equipmentJson){
        ChaincodeStub stub = ctx.getStub();
        EquipmentCO equipment = genson.deserialize(equipmentJson, EquipmentCO.class);
        String equipmentJSON = stub.getStringState(equipment.formKey());

        return (equipmentJSON != null && !equipmentJSON.isEmpty());
    }

    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public EquipmentCO CreateEquipment(final Context ctx, String equipmentJson) {
        ChaincodeStub stub = ctx.getStub();
        if (EquipmentExists(ctx, equipmentJson)) {
            String errorMessage = String.format("Equipment already exists");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        EquipmentCO equipment = genson.deserialize(equipmentJson, EquipmentCO.class);
        stub.putStringState(equipment.formKey(), equipmentJson);
        return equipment;
    }
    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public EquipmentCO ReadEquipment(final Context ctx, String equipmentId) {
        ChaincodeStub stub = ctx.getStub();
        String keyPrefiexEquipmentId = EquipmentCO.formKey(Long.valueOf(equipmentId));
        String equipmentJSON = stub.getStringState(keyPrefiexEquipmentId);
        if (equipmentJSON == null || equipmentJSON.isEmpty()) {
            String errorMessage = String.format("Equipment does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        EquipmentCO equipment = genson.deserialize(equipmentJSON, EquipmentCO.class);
        return equipment;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public EquipmentCO UpdateEquipment(final Context ctx, String equipmentJson) {
        ChaincodeStub stub = ctx.getStub();
        EquipmentCO equipment = genson.deserialize(equipmentJson, EquipmentCO.class);

        if (!EquipmentExists(ctx, equipmentJson)) {
            String errorMessage = String.format("Equipment does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        String oldEquipmentJson = stub.getStringState(equipment.formKey());
        EquipmentCO oldEquipment = genson.deserialize(oldEquipmentJson, EquipmentCO.class);
        String sortedJson = genson.serialize(equipment);
        stub.putStringState(equipment.formKey(), sortedJson);
        return oldEquipment;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public void DeleteEquipment(final Context ctx, String equipmentId) {
        ChaincodeStub stub = ctx.getStub();

        if (!EquipmentExists(ctx, equipmentId)) {
            String errorMessage = String.format("Equipment does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        stub.delState(equipmentId);
    }

}





